/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fTimeVocabularyHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 27, 2009       Raghu Sarraff			       Created
 */
package net.lwf.handler.time;

import java.util.ArrayList;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.time.util.Time;
import net.lwf.handler.time.util.TimeGenerator;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

public class TimeVocabularyHandler extends ProcessHandler implements IProcessHandler{
	
	
	public TimeVocabularyHandler() {
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return null;
	}
	
	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		int multiple = RandomNumberGenerator.generate(1, IHandlerConstant.EASY_MULTIPLES.length);
		int minutemultiple = IHandlerConstant.EASY_MULTIPLES[multiple-1];
		Time time = TimeGenerator.generateHMM(minutemultiple);

		qinfo.setAnswer("yes");
		qinfo.setRadioAnswerChoices(this.generateRadioData(time.getHours(), time.getMinutes()));
		qinfo.setHtmlQuestion(this.generateHtmlQuestion(time));
		qinfo.setPlaceHolder("What is the time now in the clock below?");
		return qinfo;
	}
	
	private List<RadioData> generateRadioData(long hours, long minutes) {
		if(hours > 12) {
			hours = hours - 12;
			if(hours > 12) {
				hours = hours - 12;
			}
		}
		String answer = String.valueOf(minutes) + " minutes after " + String.valueOf(hours);
		int type = RandomNumberGenerator.generate(1,3);
		List<RadioData> lstdata = new ArrayList<RadioData>();
		RadioData radiodata = null;
		
		if(minutes == 5 || minutes == 10 || minutes == 20 || minutes == 25) {
			if(type == 2) {
				answer = String.valueOf(minutes) + " past " + String.valueOf(hours);
			}
			else if(type == 3) {
				answer = String.valueOf(hours) + " : " + String.valueOf(minutes);
			}
			radiodata = new RadioData("no", "Quarter past " + String.valueOf(hours));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", String.valueOf(minutes) + " minutes before " + String.valueOf(hours));
			lstdata.add(radiodata);
		}
		else if(minutes == 15) {
			if(type == 2) {
				answer = "Quarter past " + String.valueOf(hours);
			}
			else if(type == 3) {
				answer = String.valueOf(hours) + " : " + String.valueOf(minutes);
			}
			radiodata = new RadioData("no", String.valueOf(minutes) + " minutes before " + String.valueOf(hours));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", String.valueOf(minutes+5) + " minutes after " + String.valueOf(hours));
			lstdata.add(radiodata);			
		}
		else if(minutes == 30) {
			if(type == 2) {
				answer = "Half past " + String.valueOf(hours);
			}
			else if(type == 3) {
				answer = String.valueOf(hours) + " : " + String.valueOf(minutes);
			}
			radiodata = new RadioData("no", String.valueOf(minutes) + " minutes before " + String.valueOf(hours-1));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", String.valueOf(minutes) + " minutes after " + String.valueOf(hours+1));
			lstdata.add(radiodata);
		}
		else if(minutes == 35 || minutes == 40) {
			if(type == 2) {
				answer = String.valueOf(60 - minutes) + " minutes before " + String.valueOf(hours+1);
			}
			else if(type == 3) {
				answer = String.valueOf(hours) + " : " + String.valueOf(minutes);
			}
			radiodata = new RadioData("no", "Half past " + String.valueOf(hours));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", "Quarter past " + String.valueOf(hours));
			lstdata.add(radiodata);
		}
		else if(minutes == 45) {
			if(type == 2) {
				answer = " Quarter to " + String.valueOf(hours + 1);
			}
			else if(type == 3) {
				answer = String.valueOf(60 - minutes) + " minutes before " + String.valueOf(hours + 1);
			}
			radiodata = new RadioData("no", String.valueOf(minutes) + " minutes before " + String.valueOf(hours-1));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", "Half past " + String.valueOf(hours));
			lstdata.add(radiodata);
		}
		else  {
			//minutes == 50 or minutes == 55
			if(type == 2) {
				answer = String.valueOf(hours) + " : " + String.valueOf(minutes);
			}
			else if(type == 3) {
				answer = String.valueOf(minutes) + " minutes before " + String.valueOf(hours + 1);
			}
			radiodata = new RadioData("no", "Quarter past " + String.valueOf(hours));
			lstdata.add(radiodata);
			radiodata = new RadioData("no", String.valueOf(minutes+5) + " minutes after " + String.valueOf(hours));
			lstdata.add(radiodata);
		}		
		
		radiodata = new RadioData("yes", answer);
		lstdata.add(radiodata);
		
		return lstdata;
	}
	
	
	private String generateHtmlQuestion(Time time) {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td>");
					sbQBlock.append("<div class=\"time_clock\" >");
						sbQBlock.append("<img src=\"" + time.getImagePath() + "\" >");
					sbQBlock.append("</div>");
				sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
        
        return sbQBlock.toString();	
	}
	
}

